/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	Administrator 
 * 
 * <p>描   述  :   MessageinfoAction.java for com.pengtu.web.app    </p>
 * 
 * <p>最后修改 : $: 2011-7-27-下午03:14:50 v 1.0.0	 Administrator   $     </p>
 * 
*/

package com.pengtu.web.app;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.app.MessageInfo;
import com.pengtu.security.SpringSecurityUtils;
import com.pengtu.service.app.SystemManager;
import com.pengtu.utils.DateUtils;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 我的消息管理Action.
 * 
 * 
 * @author cp
 */
@Results({ @Result(name = BaseActionSupport.RELOAD, location = "${ctx}/app/messageinfo.action?mm=${mm}&fontype=${fontype}&sr=${sr}", type = "redirect") })
public class MessageinfoAction extends BaseActionSupport<MessageInfo> {
	/**
	 * serialVersionUID:TODO
	 *
	 * @since 1.0.0
	 */
	
	private static final long serialVersionUID = 1L;
	private SystemManager systemManager;
	private MessageInfo messageInfo;
	private Attachment attachmention;
	private Page<MessageInfo> page = new Page<MessageInfo>(6);// 每页10条记录
	// -- 页面属性 --//
	private String id;
	private String fontype;
	private String[] items;
	private String mm;//昨天和今天以及更早记录的标记，1--今天，2--昨天，3--更早
	private String sr;//状态区别发送人还是接收人  s--发送人，r--接收人
	
	@Autowired
	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}

	
	public String getSr() {
		return sr;
	}

	public void setSr(String sr) {
		this.sr = sr;
	}

	public String getFontype() {
		return fontype;
	}

	public void setFontype(String fontype) {
		this.fontype = fontype;
	}

	public void setMm(String mm) {
		this.mm = mm;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	public MessageInfo getMessageInfo() {
		return messageInfo;
	}

	public void setMessageInfo(MessageInfo messageInfo) {
		this.messageInfo = messageInfo;
	}


	public Page<MessageInfo> getPage() {
		return page;
	}

	public void setPage(Page<MessageInfo> page) {
		this.page = page;
	}


	public Attachment getAttachmention() {
		return attachmention;
	}

	public void setAttachmention(Attachment attachmention) {
		this.attachmention = attachmention;
	}
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	@Override
	public MessageInfo getModel() {
		// TODO Auto-generated method stub
		return messageInfo;
	}

	@Override
	public String list() throws Exception {
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = systemManager.findMessageInfos(fontype,mm,sr,page,queryFilter);
		return SUCCESS;
	}

	public String getMm() {
		return mm;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}
	
	
	/** 保存*/
	@Override
	public String save() throws Exception {
		String userIds = Struts2Utils.getParameter("Ids");
		String[] uIdList = null;
		if(userIds.contains(",")){
			uIdList = userIds.split(",");
			systemManager.saveMsgInfos(uIdList,messageInfo,attachmention);
		}
		else{
			String[] uIdList2 = {userIds};
			systemManager.saveMsgInfos(uIdList2,messageInfo,attachmention);
		}
		return RELOAD;
		}

	@Override
	public String delete() throws Exception {
		String del = null;
		if(sr != null){
			if(sr.equals("s")){
				del="senddel";
			}else {
				del="receivedel";
			}
		} else{
			del="receivedel";
		}
		for (int i = 0; i < items.length; i++) {
			String mesId = items[i];
			systemManager.deleteMessageInfo(mesId,del);
			}
		return RELOAD;
	}

	public String view() throws Exception{
		if(sr != null){
			if(!sr.equals("s")){
				messageInfo.setReceiveDate(DateUtils.getCurrentDate());
				messageInfo.setReadFlag("1");
				systemManager.saveMessageInfo(messageInfo);
			}
		}
		return VIEW;
	}
	
	
	public void prepareView() throws Exception{
		prepareModel();
	}
	
	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			messageInfo = systemManager.getMessageInfo(id);
		} else {
			messageInfo = new MessageInfo();
		}
	}
	
	/**
	 * 
	 * getNoReadCount:得到没有阅读的记录
	 * 适用: 页面中消息盒子
	 * @exception 
	 * @since  1.0.0
	 */
	public void getNoReadCount(){
		String userId = SpringSecurityUtils.getCurrentUserId();
		long con = systemManager.getNoReadCount(fontype,userId);

		Struts2Utils.renderText(Long.toString(con));
	}
}
